﻿namespace KeyWordSearch.TreeParser
{

	internal static class Optimizer
	{
		public static bool ShortCutOptimizer(AstNode node)
		{
			for (int i = 0; i < node.Count; i++)
			{
				if (ShortCutOptimizer(node[i]))
				{
					i = 0;
				}
			}

			if (node.Parent != null &&
	node.Parent.Value.Attribute == TokenType.OperatorShortCutedAND &&
	node.Value.Attribute == TokenType.OperatorShortCutedAND)
			{
				for (int i = 0; i < node.Count; i++)
				{
					node.Parent.AddChilde(node[i]);
				}
				node.Parent.DeleteChild(node);

				return true;
			}
			else if (node.Parent != null &&
	node.Parent.Value.Attribute == TokenType.OperatorShortCutedOR &&
	node.Value.Attribute == TokenType.OperatorShortCutedOR)
			{
				for (int i = 0; i < node.Count; i++)
				{
					node.Parent.AddChilde(node[i]);
				}

				node.Parent.DeleteChild(node);

				return true;
			}

			return false;
		}

	}
}
